package com.sanwu.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanwu.dto.BusinessDto;
import com.sanwu.entity.Business;
import com.sanwu.mapper.BusinessMapper;
import com.sanwu.service.BusinessService;
import com.sanwu.service.UtilService;
import com.sanwu.vo.AjaxResult;
import com.sanwu.vo.CommonMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author hgh
 * @date 2025/3/11
 * @apiNote
 */

@Slf4j
@Service
public class BusinessServiceServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Resource
    private BusinessMapper businessMapper;

    @Resource
    private UtilService utilService;

    /**
     * 新增业务
     *
     * @return
     */
    @Override
    public AjaxResult insertBusiness(BusinessDto businessDto) {
        //校验是否已有重复且为完成的执行流程
        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Business::getIsDelete, 0);
        queryWrapper.eq(Business::getCompleteStatus, 0);
        queryWrapper.eq(Business::getLicenseType, 0);
        List<Business> businesses = businessMapper.selectList(queryWrapper);
        if (businesses != null && businesses.size() > 0) {
            return businessDto.getLicenseType() == 1
                    ? new AjaxResult(CommonMessage.annualReportError, "已有”执照年报“流程执行中")
                    : new AjaxResult(CommonMessage.logOffError, "已有”执照注销“流程执行中");
        }

        //新增业务
        int insert = businessMapper.insert(this.dealBasicInfo(businessDto));
        return insert == 1
                ? new AjaxResult(CommonMessage.success, "操作成功")
                : new AjaxResult(CommonMessage.error, "操作失败");
    }

    /**
     * 查看业务执行进度
     *
     * @param businessDto
     * @return
     */
    @Override
    public AjaxResult queryBusiness(BusinessDto businessDto) {
        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Business::getIsDelete, 0);
        queryWrapper.eq(Business::getId, businessDto.getId());
        List<Business> businesses = businessMapper.selectList(queryWrapper);

        if (businesses == null || businesses.size() <= 0) {
            return new AjaxResult(CommonMessage.error, "系统错误，请联系管理员");
        }

        return new AjaxResult(CommonMessage.success, "查询成功", businesses.get(0));
    }

    /**
     * 处理业务
     *
     * @param businessDto
     * @return
     */
    @Override
    public AjaxResult dealBusiness(BusinessDto businessDto) {
        LambdaUpdateWrapper<Business> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Business::getIsDelete, 0);
        updateWrapper.eq(Business::getId, businessDto.getId());
        updateWrapper.eq(businessDto.getPayStatus() != null, Business::getPayStatus, businessDto.getPayStatus());

        //基础信息赋值
        int update = businessMapper.update(this.dealBasicInfo(businessDto), updateWrapper);
        return update >= 1 ? new AjaxResult(CommonMessage.success, "处理完成") : new AjaxResult(CommonMessage.error, "处理失败");
    }

    /**
     * 基础信息赋值
     *
     * @param businessDto
     * @return
     */
    private BusinessDto dealBasicInfo(BusinessDto businessDto) {
        if (businessDto.getId() == null || "".equals(businessDto.getId())) {
            businessDto.setId(IdUtil.simpleUUID());
            businessDto.setCreateTime(new Date());
        }

        businessDto.setUpdateTime(new Date());
        return businessDto;
    }
}
